home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-24 | 7.6 KB | 162 lines | [TEXT/ALFA] |
-
- Answers
- -------
-
- Answers to lesson exercises. Try the exercises _before_ reading these!
- (please...)
-
-
- ================================================================================
-
-
- Lesson 1
- --------
-
- 1.1 : nip ( a b -- b ) swap drop ;
- 1.2 : over ( a b -- a b a ) swap dup rot swap ;
- 1.3 : tuck ( a b -- b a b ) dup rot swap ;
- 1.4 : 2dup ( a b -- a b a b ) over over ;
- or swap dup rot swap swap dup rot swap ;
-
- 1.5 32 4 mod 55 55 * 102 / - 45 34 * 3 4 - 5 - / + .
- 1.6 1323 1023 102 45 / / / 3 2 9 * * 4 / mod .
-
- 1.7 : ^2 ( a -- a^2 ) dup * ;
- 1.8 : ^3 ( a -- a^3 ) dup dup * * ;
- 1.9 : /mod ( a b -- a/b (a mod b) ) 2dup / rot rot swap mod ;
- (or swap dup rot swap swap dup rot swap / rot rot swap mod ; )
- (note: /mod is present in most forth systems )
-
- 1.10 a) Yes. SWAP places the 3 and the sum (34+23) in the correct order.
- b) No. 33 12 56 - + = 33+(12-56) <> 56 12 33 + - = 56-(12+33)
-
- 1.11 The product 52 * 654 = 34008 which in a 16-bit Forth overflows and
- becomes -31528. Then 1024 / gives -30. Part (a) works because */
- uses a 32-bit multiply preserving the 34008.
-
-
-
-
- Lesson 2
- --------
-
- 2.1 : blast1 ( -- ) 11 0 do i . cr loop ." BLAST OFF!!!" cr ;
- 2.2 : blast2 ( -- ) -1 begin 1+ dup 11 < while dup . cr repeat
- drop ." BLAST OFF!!!" cr ;
- 2.3 : blast3 ( -- ) 0 begin dup . cr 1+ dup 11 = until
- drop ." BLAST OFF!!!" cr ;
-
- 2.4 : typeKEY ( -- )
- key
- dup 13 = if drop ." <CR>" cr else
- dup 27 = if drop ." <ESC>" else
- dup 27 < if ." <CTRL-" 64 + emit ." >" else
- dup 31 > if emit else
- drop then then then then ;
-
- 2.5 : +CONST ( i0 i1 i2 k -- i0+k i1+k i2+k )
- dup >r + rot R@ + rot r> + rot ;
-
-
-
-
- Lesson 3
- --------
-
- 3.1 variable L
- variable W
- variable H
-
- : volLWH ( -- ) L @ W @ H @ * * ;
-
- : SurArea ( -- ) L @ W @ * 2* L @ H @ * 2* W @ H @ * 2* + + ;
-
- : AVratio ( vol area -- vol/area vol mod area )
- dup >r swap dup r> / rot rot swap mod ;
-
- : VOLUME.STATS ( L W H -- )
- H ! W ! L !
- cr ." LENGTH = " L @ . ." m, WIDTH = " W @ .
- ." m, HEIGHT = " H @ . ." m" cr
- cr ." VOLUME = " volLWH dup . ." cubic meters"
- cr ." SURFACE AREA = " SurArea dup . ." square meters" cr
- dup >r AVratio swap
- cr ." RATIO VOLUME / SURFACE AREA = " .
- dup 0= if drop r> drop else ." and " . ." /" r> . then cr ;
-
-
- 3.2 : { ; ( make the code look nicer )
- : x ;
-
- : MATRIX ( addr i j -- ) ( reserve room for the matrix and keep the )
- over over ( 2dup ) ( indices in the first two locations )
- * 2* 4 + allot
- rot dup >r 2+ ! r> ! ;
-
- : } ( addr i j -- address-of-i,j-th-element )
- rot dup >r @ rot * + 2* 4 + r> + ;
-
-
- 3.3 : 2dup ( a b -- a b a b ) over over ;
-
- : M! ( 00 01 10 11 addr -- ) ( store values in a 2 x 2 matrix )
- dup >r { 1 1 } ! R@ { 1 0 } ! R@ { 0 1 } ! r> { 0 0 } ! ;
-
- : M+ ( a b -- 00 01 10 11 ) ( add two 2 x 2 matrices )
- 2dup { 0 0 } @ swap { 0 0 } @ + rot rot
- 2dup { 0 1 } @ swap { 0 1 } @ + rot rot
- 2dup { 1 0 } @ swap { 1 0 } @ + rot rot
- { 1 1 } @ swap { 1 1 } @ + ;
-
- : .M ( addr -- ) ( print a 2 x 2 matrix )
- ." [[ " dup >r { 0 0 } @ . space R@ { 0 1 } @ . ." ],[ "
- R@ { 1 0 } @ . space r> { 1 1 } @ . ." ]]" ;
-
-
- 3.4 variable addr ( address of string )
- variable len ( length of string )
- : REVERSE ( addr len -- ) ( reverse a string )
- len ! addr !
- len @ 2/ 0 do ( swap outermost characters, then )
- addr @ i + c@ ( the next ones in, etc. )
- addr @ len @ + 1- i - c@
- addr @ i + c!
- addr @ len @ + 1- i - c!
- loop ;
-
-
-
-
- Lesson 4
- --------
-
- ( Exercise 4.1 )
- ( Read 10 lines into a buffer and write the buffer to disk )
-
- ( structures )
-
- create BUFFER 10 80 * allot ( room for 10 lines of up to 80 characters )
- create STR 80 allot ( the input string )
-
- variable ptr ( point to the address for the beginning of the next line )
-
- variable addr ( for >buffer to make it clearer )
- variable len
-
- ( definitions )
-
- : input ( addr -- len ) ( put a string terminated with <cr> in addr )
- dup >r ( save the address )
- 79 expect cr ( get the string leaving room for terminating <cr> )
- r> 13 swap span + c! ( put in terminating <cr> )
- span 1+ ; ( return length including <cr> )
-
- : >buffer ( len addr -- ) ( copy into the buffer at current pointer )
- addr ! len ! ( save values )
- BUFFER ptr @ + len @ + BUFFER ptr @ + do
- addr @ i + BUFFER ptr @ + - c@ ( get a character )
- i c! ( save it in the buffer )
- loop
- ptr @ len @ + ptr ! ; ( bump pointer )
-
- : getText ( -- ) ( read 10 l